package com.rocksea.checkservice;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;

import java.io.File;
import java.util.ArrayList;
import java.util.UUID;

/**
 * Created by Mrw on 2017/8/19.
 *
 */

public class DBManger
{
    private static SQLiteDatabase mDatabase;
    private DBManger()
    {}
    public static void init(Context context)
    {
        String path = Environment.getExternalStorageDirectory().getPath()+"/db/";
        File file = new File(path);
        if (!file.exists())
        {
            if (!file.mkdirs())
                try {
                    throw new Exception("创建数据失败");
                } catch (Exception e) {
                    e.printStackTrace();
                }
        }

        DBHelper dbHelper = new DBHelper(context,path+"Service.db");
        mDatabase = dbHelper.getWritableDatabase();
    }

    public static void insert(ArrayList<ServiceInfo> services)
    {
        for (ServiceInfo service : services)
        {
            ContentValues values = new ContentValues();
            values.put("uuid",UUID.randomUUID().toString());
            values.put("ServerIp",service.getServerIp());
            values.put("DtuPort",service.getDtuPort());
            values.put("RegionName",service.getRegionName());
            values.put("WebIp",service.getWebIp());
            values.put("WebPort",service.getWebPort());
            values.put("Flag",service.getFlag());
            values.put("WebFlag",service.isWebFlag()?1:0);
            values.put("Province",service.getProvince());
            values.put("TransferFlag",service.getTransferFlag());
            values.put("isValid",1);
            mDatabase.insert("Service",null,values);
        }
    }



}
